from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.core.context_processors import csrf
from django.template import RequestContext
from cars.models import *
from documents.models import documentForm
from django.contrib.auth.decorators import login_required

@login_required
def index(request):
    cars =  Car.objects.all().order_by('-expiry_date')
    return render_to_response('Cars/index.html', {'cars': cars})
    
@login_required
def brands(request):
    brands =  CarBrand.objects.all()
    return render_to_response('Cars/brands.html', {'brands': brands})
    
@login_required
def validateNewBrand(request):
    if request.method == 'POST': # If the form has been submitted...
        car = CarBrand()
        form = BrandForm(request.POST, instance=car) # A form bound to the POST data
        if form.is_valid():
#            if car.checkup_expiry == None:
            form.save(commit = True);
            return HttpResponseRedirect('../brands') # Redirect after POST
    else:
        car = CarBrand()
        form =  BrandForm(request.POST, instance=car )
        return render_to_response('Cars/addBrand.html', {'person': form})
        

    
@login_required
def addBrand(request):

    form =  BrandForm( )
    c = {}
    c.update(csrf(request))
    return render_to_response('Cars/addBrand.html', {'b': form},context_instance=RequestContext(request) )
    
        
@login_required
def detail(request, idno):
    car = Car.objects.get(chase_no = idno)
    docs = car.documents.all()
    return render_to_response('Cars/details.html', {'car': car, 'docs': docs})
@login_required
def docDetail(request, idno, docId):
    car = Car.objects.get(chase_no = idno)
    doc = car.documents.get(id = docId)
    return render_to_response('Cars/docDetails.html', {'car': car, 'doc': doc})

@login_required
def edit(request, idno):
    car = Car.objects.get(chase_no = idno)

    form =  CarForm(instance=car )
    c = {}
    c.update(csrf(request))
    return render_to_response('Cars/edit.html', {'car': form, 'org': car},context_instance=RequestContext(request) )
@login_required
def addDoc(request, idno):
    car = Car.objects.get(chase_no = idno)
    form =  documentForm()
    c = {}
    c.update(csrf(request))
    return render_to_response('Cars/addDoc.html', {'doc': form, 'org': car},context_instance=RequestContext(request) )
@login_required
def validate(request, idno):
    if request.method == 'POST': # If the form has been submitted...
        car = Car.objects.get(chase_no = idno)
        form = CarForm(request.POST, instance=car) # A form bound to the POST data
        if form.is_valid():
#            if car.checkup_expiry == None:
            if car.arabic_check_day != None:
                car.arabic_to_gorg_check()
#            if car.expiry_date == None:
            if car.arabic_reg_day != None:
                car.arabic_to_gorg_reg()


            form.save(commit = True);
            return HttpResponseRedirect('../details/') # Redirect after POST
    else:
        car = Car.objects.get(chase_no = idno)

        form =  CarForm(request.POST, instance=car )

        return render_to_response('Cars/edit.html', {'person': form, 'org': car})

@login_required
def validateNew(request):
    if request.method == 'POST': # If the form has been submitted...
        car = Car()
        form = CarForm(request.POST, instance=car) # A form bound to the POST data
        if form.is_valid():
#            if car.checkup_expiry == None:
            if car.arabic_check_day != None:
                car.arabic_to_gorg_check()
#            if car.expiry_date == None:
            if car.arabic_reg_day != None:
                car.arabic_to_gorg_reg()

            form.save(commit = True);
            return HttpResponseRedirect('../') # Redirect after POST
    else:
        car = Car()

        form =  CarForm(request.POST, instance=car )

        return render_to_response('Cars/edit.html', {'person': form})
@login_required
def add(request):

    form =  CarForm( )
    c = {}
    c.update(csrf(request))
    return render_to_response('Cars/add.html', {'car': form},context_instance=RequestContext(request) )
@login_required
def validateDoc(request, idno):

    if request.method == 'POST': # If the form has been submitted...
        doc = Document()
        car = Car.objects.get(chase_no = idno)
        form = documentForm(request.POST, request.FILES, instance=doc)

        if form.is_valid():
            doc.arabic_to_gorg()
            doc.save()
            car.documents.add(doc)
            car.save()
            return HttpResponseRedirect('../details/') # Redirect after POST
        else:
            car = Car.objects.get(chase_no = idno)
            form =  documentForm(request.POST)

            c = {}
            c.update(csrf(request))
            return render_to_response('Cars/addDoc.html', {'doc': form, 'org': car},context_instance=RequestContext(request) )
